function get_page(href, target){
	$.ajax({
		url: href,
		success: function(response){
			$('#'+target).html(response)
		}
	})
}

function get_asyncronous_array_links(array_links){
		array_links.bind("click",function(event){
		event.preventDefault();
		el = $(this)
		get_page(el.attr('href'), el.attr('target'))
		window.location.replace("#"+el.attr('title')+"/")
		})
	//
	}

function print_json_gallery(json){
	/**
	 * 1 - Carico il form
	 * 2 - Riempio il form con i dati json
	 */

	/**
	 * Step 1
	 */
	get_page("pages/form_gallery.php","content")
	/**
	 * Step 2
	 */

}

function pageload(hash){

	if(hash.match("gallery/@[0-9]")){
		gallery_id = hash.split("@")[1]
		if(gallery_id.search("/add") != -1){
			add_photo_to_gallery()
			gallery_id = gallery_id.replace("/add","")
		}

		gallery_str_bar()
		show_gallery()
	}
	if(hash.match("gallery")){
		$("a#help").attr({
			"href": "#help.php?page="+hash,
			"title": "help.php?page="+hash
		})
	}

	if (hash == "gallery" || hash == "" || hash == "index") {
			galleries_index()
			gallery_str_bar()
	}
	if (hash == "gallery/add") {
		galleries_index()
		add_gallery();
	}
	if(hash == "settings"){
		JSONdata = get_JSON("scripts/get_json_user.php")
		user_edit(JSONdata)
		$("#gallery_user").submit(function(event){
			event.preventDefault()
			$.ajax({
				url: $(this).attr("action"),
				data: $(this).serialize(),
				type: "POST"
			})
		})
		$("a#help").attr({
			"href": "#help.php?page="+hash,
			"title": "help.php?page="+hash
		})
	}
	if(hash == "events"){
		events_str_bar()
		events_index()
		$("a#help").attr({
			"href": "#help.php?page="+hash,
			"title": "help.php?page="+hash
		})
	}

	if(hash == "events/add"){
		add_event()
		events_index()
		$("a#help").attr({
			"href": "#help.php?page=events",
			"title": "help.php?page=events"
		})
	}

	if(hash.match("help")){
		$("#content").html("").append($("<iframe />").attr({
			"src": "http://www.nerdriot.it/pages/"+hash
		}).css({
			"width": "100%",
			"height": "700px",
			"border": "0px"
		}))
	}

	if(hash == "bug"){
		$.ajax({
			url:"/pages/bug_report_form.php",
			success: function(res){
				$("#content").html(res)
			}
		})
	}
	if(hash == "logout"){
		window.location.assign("logout.php")
	}
}

$(document).ready(function(){

	/*
	Avverte l'utente che il sito sta caricando qualcosa
	*/
	$("#loading").ajaxStart(function(){
		$(this).show()
	})
	$("#loading").ajaxComplete(function(){
		$(this).hide()
	})
	/*
	##########################
	*/

	/*
	Scorciatoie da tastiera
	*/
	$(document).bind("keydown",{combi: "e", disableInInput: true},function(){
		window.location.replace("#events")
	})

	$(document).bind("keydown",{combi: "g", disableInInput: true},function(){
		window.location.replace("#gallery")
	})

	$(document).bind("keydown",{combi: "h", disableInInput: true},function(){
		window.location.replace("#help")
	})

	$(document).bind("keydown",{combi: "s", disableInInput: true},function(){
		window.location.replace("#settings")
	})

	$(document).bind("keydown",{combi: "a", disableInInput: true},function(){
		window.location.replace($("#add_btn").attr("href"))
	})

	$(document).bind("keydown",{combi: "esc", disableInInput: true},function(){
		loc = new String(window.location)
		loc = loc.replace("/add","")
		window.location.replace(loc)
	})
	/*
	##########################
	*/

	$.historyInit(pageload);
	$("a").click(function(event){
		event.preventDefault() // Stoppo il normale caricamento dei link
		window.location.replace("#"+$(this).attr("title"))
		var hash = this.title
		hash = hash.replace(/^.*#/, '')
		$.historyLoad(hash)
		return false
	})
})